//
//  UserInfoViewController.m
//  OfficeBook
//
//  Created by 孙晓奇 on 15/10/1.
//  Copyright © 2015年 sunny. All rights reserved.
//

#import "UserInfoViewController.h"
#import "LoginViewController.h"
#import "UserModel.h"

static NSString * const kDefaultCellIdentifer = @"DefaultCellIdentifer";

@interface UserInfoViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UserModel   *userInfo;
@property (nonatomic, strong) NSArray     *titleArray;
@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"User Information";
    
    // close btn
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    closeBtn.frame = CGRectMake(0, 0, 44, 44);
    closeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [closeBtn setTitle:@"Close" forState:UIControlStateNormal];
    [closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    closeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [closeBtn addTarget:self action:@selector(dismissVC) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBarItem = [[UIBarButtonItem alloc] initWithCustomView:closeBtn];
    self.navigationItem.leftBarButtonItem = backBarItem;
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#ebebeb"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //table footer
    [self initTableFooterView];
    [self.view addSubview:self.tableView];
    
    //get userInfo
    self.userInfo = [[UserModel alloc] init];
    [self getUserInfo];
    
    self.titleArray = @[@"Username", @"Name", @"Email"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

-(void)loginOut {
    [UserDefaultDataHelper removeDataWithKey:UserDefaultKeyUserEmail];
    [UserDefaultDataHelper removeDataWithKey:UserDefaultKeyUserfirstName];
    [UserDefaultDataHelper removeDataWithKey:UserDefaultKeyUserlastName];
    [UserDefaultDataHelper removeDataWithKey:UserDefaultKeyUserUserName];
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationBar"] forBarMetrics:UIBarMetricsDefault];
    nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    self.view.window.rootViewController = nav;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDefaultCellIdentifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kDefaultCellIdentifer];
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    switch (indexPath.row) {
        case 0:
            cell.detailTextLabel.text = self.userInfo.userName;
            break;
        case 1:
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", self.userInfo.firstName, self.userInfo.lastName];
            break;
        case 2:
            cell.detailTextLabel.text = self.userInfo.email;
            break;
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 18.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}


-(void)initTableFooterView {
    UIView *footbgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 120)];
    footbgView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = footbgView;
    
    UIButton *loginBtn    = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.tableView addSubview:loginBtn];
    
    [loginBtn setTitle:@"Logout" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    UIImage *image = [PureColorImageHelper imageWithColor:@"#e2513c" withHeight:44 withWidth:self.tableView.frame.size.width - 30];
    [loginBtn setBackgroundImage:image forState:UIControlStateNormal];
    loginBtn.layer.cornerRadius = 5.f;
    loginBtn.layer.masksToBounds = YES;
    [loginBtn addTarget:self action:@selector(loginOut) forControlEvents:UIControlEventTouchUpInside];

    [loginBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(footbgView.centerX);
        make.top.equalTo(footbgView.top).offset(20);
        make.width.equalTo(@(image.size.width));
        make.height.equalTo(@(image.size.height));
    }];
}

-(void)dismissVC {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)getUserInfo {
    self.userInfo.userName = [UserDefaultDataHelper getDataWithKey:UserDefaultKeyUserUserName];
    self.userInfo.firstName = [UserDefaultDataHelper getDataWithKey:UserDefaultKeyUserfirstName];
    self.userInfo.lastName = [UserDefaultDataHelper getDataWithKey:UserDefaultKeyUserlastName];
    self.userInfo.email = [UserDefaultDataHelper getDataWithKey:UserDefaultKeyUserEmail];
}
@end
